package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.system.domain.BarcodeFieldLine;
import com.ruoyi.system.domain.BarcodePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BarcodePageFieldMappingMapper;
import com.ruoyi.system.domain.BarcodePageFieldMapping;
import com.ruoyi.system.service.IBarcodePageFieldMappingService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 条码页面字段映射Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-26
 */
@Service
public class BarcodePageFieldMappingServiceImpl implements IBarcodePageFieldMappingService 
{
    @Autowired
    private BarcodePageFieldMappingMapper barcodePageFieldMappingMapper;

    /**
     * 查询条码页面字段映射
     * 
     * @param id 条码页面字段映射主键
     * @return 条码页面字段映射
     */
    @Override
    public BarcodePageFieldMapping selectBarcodePageFieldMappingById(Long id)
    {
        return barcodePageFieldMappingMapper.selectBarcodePageFieldMappingById(id);
    }

    /**
     * 查询条码页面字段映射列表
     * 
     * @param barcodePageFieldMapping 条码页面字段映射
     * @return 条码页面字段映射
     */
    @Override
    public List<BarcodePageFieldMapping> selectBarcodePageFieldMappingList(BarcodePageFieldMapping barcodePageFieldMapping)
    {
        return barcodePageFieldMappingMapper.selectBarcodePageFieldMappingList(barcodePageFieldMapping);
    }

    /**
     * 新增条码页面字段映射
     * 
     * @param barcodePageFieldMapping 条码页面字段映射
     * @return 结果
     */
    @Override
    public int insertBarcodePageFieldMapping(BarcodePageFieldMapping barcodePageFieldMapping)
    {
        return barcodePageFieldMappingMapper.insertBarcodePageFieldMapping(barcodePageFieldMapping);
    }

    /**
     * 修改条码页面字段映射
     * 
     * @param barcodePageFieldMapping 条码页面字段映射
     * @return 结果
     */
    @Override
    public int updateBarcodePageFieldMapping(BarcodePageFieldMapping barcodePageFieldMapping)
    {
        return barcodePageFieldMappingMapper.updateBarcodePageFieldMapping(barcodePageFieldMapping);
    }

    /**
     * 批量删除条码页面字段映射
     * 
     * @param ids 需要删除的条码页面字段映射主键
     * @return 结果
     */
    @Override
    public int deleteBarcodePageFieldMappingByIds(Long[] ids)
    {
        return barcodePageFieldMappingMapper.deleteBarcodePageFieldMappingByIds(ids);
    }

    /**
     * 删除条码页面字段映射信息
     * 
     * @param id 条码页面字段映射主键
     * @return 结果
     */
    @Override
    public int deleteBarcodePageFieldMappingById(Long id)
    {
        return barcodePageFieldMappingMapper.deleteBarcodePageFieldMappingById(id);
    }

    @Override
    @Transactional
    public int saveBarcodeFieldLine(BarcodePage barcodePage) {
        List<BarcodePageFieldMapping> pageFieldMappingList = barcodePage.getPageFieldMappingList();
        List<BarcodePageFieldMapping> originpageFieldMappingList = barcodePageFieldMappingMapper.selectBarcodePageFieldMappingByPageId(barcodePage.getId());
        List<Long> x1 = pageFieldMappingList.stream().map(BarcodePageFieldMapping::getId).collect(Collectors.toList());
        List<Long> x2 = originpageFieldMappingList.stream().map(BarcodePageFieldMapping::getId).collect(Collectors.toList());

        // 计算 x2 - x1 的差集
        List<Long> toDeletes = x2.stream()
                .filter(id -> !x1.contains(id)) // 过滤掉 x1 中存在的元素
                .collect(Collectors.toList());

        for (BarcodePageFieldMapping pagefield : pageFieldMappingList) {
            if (pagefield.getId() == null ) {
                pagefield.setPageId(barcodePage.getId());
                pagefield.setCreatedBy(barcodePage.getCreateBy());
                pagefield.setCreatedDate(new Date());
                barcodePageFieldMappingMapper.insertBarcodePageFieldMapping(pagefield);
            }else{
                pagefield.setModifiedBy(barcodePage.getUpdateBy());
                pagefield.setModifiedDate(new Date());
                barcodePageFieldMappingMapper.updateBarcodePageFieldMapping(pagefield);
            }
        }

        if(!toDeletes.isEmpty()) {
            barcodePageFieldMappingMapper.deleteBarcodePageFieldMappingByIds(toDeletes.toArray(new Long[0]));
        }
        return 1;
    }
}
